﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceTaskTimeFixed
    {


        EFYihongIotContext ef = new EFYihongIotContext();
        public iot_device_task_time_fixed getModel(string guid)
        {
            return ef.iot_device_task_time_fixed.Where(c => c.tf_guid == guid).FirstOrDefault();
        }

        public void delModel(string guid)
        {
            ef.iot_device_task_time_fixed.Where(c => c.tf_guid == guid).ExecuteDelete();
        }

        public List<iot_device_task_time_fixed> getListByClient(string cguid)
        {
            return ef.iot_device_task_time_fixed.Where(c => c.tf_c_guid == cguid).ToList();
        }
        public List<iot_device_task_time_fixed> getListByProduct(string pguid)
        {
            return ef.iot_device_task_time_fixed.Where(c => c.tf_p_guid == pguid).ToList();
        }


        public bool saveAll(iot_device_task_time_fixed req)
        {
            req.tf_start_time = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + req.tf_time_value);
            if (req.tf_start_time < DateTime.Now)
            {
                req.tf_start_time = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " " + req.tf_time_value);
            }
            //先查一下有没重复提交
            iot_device_task_time_fixed model = ef.iot_device_task_time_fixed.Where(c => c.tf_guid == req.tf_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.tf_add_time = DateTime.Now;
                req.tf_mod_time = DateTime.Now;
                // req.tf_start_time = DateTime.Now;
                req.tf_time_name = "时间";
                req.tf_is_template = 0;
                req.tf_auto_run = 0;
                req.tf_auto_run_jion = 0;
                req.tf_interval_day = 0;
                req.tf_execute_time = DateTime.Parse("2000-01-01 00:00:00");
                // req.ti_execute_count = 0;

                //req.tf_c_guid = LoginUserInfo.userCompanyGuid;
                if (!string.IsNullOrEmpty(req.tf_p_guid))
                {
                    req.tf_is_template = 1;
                    req.tf_c_guid = ef.iot_product.Where(c => c.p_guid == req.tf_p_guid).FirstOrDefault().p_c_guid;
                }
                ef.iot_device_task_time_fixed.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.tf_start_time = req.tf_start_time;
                model.tf_title = req.tf_title;
                model.tf_status = req.tf_status;
                model.tf_time_value = req.tf_time_value;
                model.tf_key = req.tf_key;
                model.tf_task_guid = req.tf_task_guid;
                model.tf_interval_day= req.tf_interval_day;
                //model.tf_repeat_count = req.tf_repeat_count;
                model.tf_mod_time = DateTime.Now;
                ef.iot_device_task_time_fixed.Update(model);
                ef.SaveChanges();
            }
            return true;
        }
        public bool changeStatus(string tf_guid)
        {
            iot_device_task_time_fixed model = ef.iot_device_task_time_fixed.Where(c => c.tf_guid == tf_guid).FirstOrDefault();
            if (model != null)
            {
                if (model.tf_status == 0)
                {
                    model.tf_status = 1;
                }
                else
                {
                    model.tf_status = 0;
                }
                ef.SaveChanges();
            }
            return true;
        }
        public void del(string tf_guid)
        {
          
                ef.iot_device_task_time_fixed.Where(c => c.tf_guid == tf_guid).ExecuteDelete();
                ef.SaveChanges();
           

        }


    }
}
